package com.shujia.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

/**
 * @author shujia
 */
public class Demo3KafkaConsumer {
    public static void main(String[] args) throws Exception{

        Properties properties = new Properties();
        //kafka broker列表
        properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092");
        //反序列化类
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        /*
         * earliest
         * 当各分区下有已提交的offset时，从提交的offset开始消费；无提交的offset时，从头开始消费
         * latest  默认
         * 当各分区下有已提交的offset时，从提交的offset开始消费；无提交的offset时，消费新产认值生的该分区下的数据
         * none
         * topic各分区都存在已提交的offset时，从offset后开始消费；只要有一个分区不存在已提交的offset，则抛出异常
         *
         */
        //读取数据的位置
        properties.setProperty("auto.offset.reset", "earliest");

        //消费者组,同一条数据在一个组内只消费一次
        //默认是每隔5秒提交一次消费偏移量
        properties.setProperty("group.id", "Demo3KafkaConsumer4");

        //1、创建消费者
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);

        //消费数据
        //订阅消费的topic
        ArrayList<String> topics = new ArrayList<>();
        topics.add("bigdata");
        kafkaConsumer.subscribe(topics);


        //循环消费数据
        while (true) {
            //拉取数据
            ConsumerRecords<String, String> consumerRecords = kafkaConsumer.poll(200);
            //循环解析数据
            for (ConsumerRecord<String, String> record : consumerRecords) {
                String topic = record.topic();
                long offset = record.offset();
                int partition = record.partition();
                String key = record.key();
                String value = record.value();
                System.out.println(topic + "\t" + offset + "\t" + partition + "\t" + key + "\t" + value);
            }
        }



    }
}
